﻿using System.Windows.Controls;
using PSE.Deployment.ConfigurationManager.Controls.Validations;

namespace PSE.Deployment.ConfigurationManager.Controls
{
    public class BaseSimpleTextBox : TextBox
    {
        bool _isRequired;
        public bool IsRequired
        {
            get { return _isRequired; }
            set { _isRequired = value; }
        }

        public BaseSimpleTextBox()
        {
            this.PreviewLostKeyboardFocus += new System.Windows.Input.KeyboardFocusChangedEventHandler(_PreviewLostKeyboardFocus);
            this.SetResourceReference(BaseSimpleTextBox.StyleProperty, "TextBoxConfigurator");
        }

        void _PreviewLostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            //e.Handled = !this.IsValid;
            bool isValid = this.IsValid;
        }

        public bool IsValid
        {
            get
            {
                if (this.IsRequired && SpecializedControlsValidations.IsEmpty(this.Text, false))
                {
                    this.SetResourceReference(BaseSimpleTextBox.StyleProperty, "TextBoxConfiguratorRequired");
                    this.ToolTip = SpecializedControlsValidations.GetDescription(ErrorMessage.RequiredField);
                    return false;
                }
                else if (!this.Validate())
                {
                    this.SetResourceReference(BaseSimpleTextBox.StyleProperty, "TextBoxConfiguratorRequired");
                    this.ToolTip = SpecializedControlsValidations.GetDescription(this.GetErrorMessage());
                    return false;
                }
                else
                {
                    this.SetResourceReference(BaseSimpleTextBox.StyleProperty, "TextBoxConfigurator");
                    this.ToolTip = null;
                    return true;
                }
            }
        }

        protected virtual bool Validate()
        {
            return true;
        }

        protected virtual ErrorMessage GetErrorMessage()
        {
            return ErrorMessage.RequiredField;
        }
    }
}